home *** CD-ROM | disk | FTP | other *** search
/ AOL File Library: 2,801 to 2,900 / aol-file-protocol-4400-2801-to-2900.zip / AOLDLs / C++ Files Library / Graphic Gems I, II & III (C_C++) / Graphics Gems C Code.sea / GemsIII / accurate_scan / test.c < prev    next >
C/C++ Source or Header  |  1992-06-16  |  5KB  |  221 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. #include <starbase.c.h>
  5.  
  6. #ifndef M_PI
  7. #  define M_PI        3.14159265358979323846
  8. #endif
  9.  
  10. extern double drand48();
  11.  
  12. typedef int fixpoint;
  13. extern void subpixel_triangle(fixpoint x0, fixpoint y0,
  14.                                         fixpoint x1, fixpoint y1,
  15.                                         fixpoint x2, fixpoint y2);
  16. extern void triangle(int x0, int y0, int x1, int y1, int x2, int y2);
  17. extern void printnbits();
  18.  
  19. static int    fildes;
  20. static struct color {float r, g, b;};
  21. int verbose = 0;
  22.  
  23. #define MAXVERTICES     1000
  24. #define MAXTRIANGLES    1000
  25.  
  26. #define SIZE 200
  27. #define BUFSIZE SIZE*2
  28. int buffer[BUFSIZE][BUFSIZE];
  29. int which_tri;
  30.  
  31. void clear_buffer()
  32. {
  33.   register int i, j;
  34.   if (verbose) printf("\n\n************** New Iteration ************\n\n");
  35.  
  36.   for(i=0; i<BUFSIZE; i++)
  37.      for(j=0; j<BUFSIZE; j++)
  38.         buffer[i][j] = 0;
  39. }
  40.  
  41.  
  42. int
  43. InitScreen()
  44. {
  45.     char    *sb_dev, *driver;
  46.     
  47.     printnbits();
  48.  
  49.     sb_dev = getenv("SB_OUTDEV");   
  50.     if(!sb_dev) sb_dev = getenv("OUTDEV");   
  51.     if(!sb_dev) sb_dev = "/dev/crt1";
  52.  
  53.     driver = getenv("SB_OUTDRIVER");
  54.     if(!driver) driver = getenv("OUTDRIVER");
  55.     if (!driver ) driver = "hp98731";
  56.     
  57.     fildes = gopen(sb_dev,OUTDEV,driver,INIT|INT_XFORM);
  58.     interior_style(fildes,INT_SOLID,FALSE);
  59.     
  60.     intvdc_extent(fildes,0,0,SIZE, SIZE);
  61.     mapping_mode(fildes,FALSE);
  62.     
  63.     drawing_mode(fildes,6);      /* xor mode */
  64.     
  65.     if (!verbose)
  66.       double_buffer(fildes,TRUE,12); 
  67.  
  68.     clear_control(fildes, CLEAR_VIEWPORT);
  69.     
  70.     return(fildes);
  71. }
  72.  
  73. int buf = 0;
  74. int t0[MAXTRIANGLES], t1[MAXTRIANGLES], t2[MAXTRIANGLES];
  75. fixpoint x[MAXVERTICES], y[MAXVERTICES];
  76. float xf[MAXVERTICES], yf[MAXVERTICES];
  77.  
  78.  
  79. void print_triangle(int n)
  80. {
  81.   printf("Triangle %d contains points %d, %d, %d\n", n, t0[n], t1[n], t2[n]);
  82.   printf("which is (%g, %g), (%g, %g), (%g, %g)\n",
  83.             xf[t0[n]], yf[t0[n]],
  84.             xf[t1[n]], yf[t1[n]],
  85.             xf[t2[n]], yf[t2[n]]);
  86.   printf("which is ");
  87.   fp_print(x[t0[n]]);
  88.   printf(", ");
  89.   fp_print(y[t0[n]]);
  90.   printf(", ");
  91.   fp_print(x[t1[n]]);
  92.   printf(", ");
  93.   fp_print(y[t1[n]]);
  94.   printf(", ");
  95.   fp_print(x[t2[n]]);
  96.   printf(", ");
  97.   fp_print(y[t2[n]]);
  98.   printf("\n");
  99. }
  100.  
  101.  
  102. void
  103. draw_point (int ix, int iy)
  104. {
  105.  
  106. /*  if ((ix == 91) && (iy == 74)) {
  107.   printf("Triangle painting (%d, %d) ... Triangle %d\n", ix, iy, which_tri);
  108.   print_triangle(which_tri); }
  109. */
  110.  
  111.   if (buffer[ix][iy]){
  112.      printf("OOPS -- repainted pixel (%d, %d) for Triangle %d\n",
  113.               ix, iy, which_tri);
  114.      print_triangle(which_tri);
  115.  
  116.      fill_color(fildes, 1.0, 0.0, 0.5);
  117.      intrectangle(fildes,ix,iy,ix+1,iy+1);
  118.      fill_color(fildes, 1.0, 1.0, 1.0);
  119. /*     dbuffer_switch(fildes, !buf); 
  120.      make_picture_current(fildes);
  121.      abort();
  122. */
  123.   }
  124.   else {
  125.      buffer[ix][iy] = 1;
  126.      intrectangle(fildes,ix,iy,ix+1,iy+1);
  127.   }
  128. }
  129.  
  130.  
  131.  
  132. #define CENTER 0.5
  133.  
  134. main (int argc, char *argv[])
  135. {
  136.     int xi[MAXVERTICES], yi[MAXVERTICES];
  137.     struct color tc[MAXTRIANGLES];
  138.     int nv, nt, i, j, k, white;
  139.     double c, s;
  140.     float rotinc, xr, yr;
  141.      float scale = 90.0;
  142.  
  143.      char *filename = NULL;
  144.      FILE *fp;
  145.  
  146.      if (argc == 2) {
  147.         filename = argv[1];
  148.         printf("reading from file %s\n", filename);
  149.      }
  150.  
  151.      if (filename) fp = fopen(filename, "r");
  152.      else fp = stdin;
  153.  
  154.     fscanf(fp, "%f %d %d",&rotinc,&white,&nv);
  155.      printf("Read header info.\n");
  156.     for (i=0; i<nv; i++) fscanf(fp, "%f %f",&xf[i],&yf[i]);
  157.      printf("Read %d verts\n", nv);
  158.  
  159.     for (nt=0; (fscanf(fp, "%d %d %d",&t0[nt],&t1[nt],&t2[nt])!=EOF) &&
  160.             (nt < MAXTRIANGLES); nt++) {
  161.         if (white) 
  162.           tc[nt].r = tc[nt].g = tc[nt].b = 1.0;
  163.         else
  164.           tc[nt].r = drand48(), tc[nt].g = drand48(), tc[nt].b = drand48();
  165.      }
  166.  
  167.      printf("Read %d triangles\n", nt);
  168.  
  169.     InitScreen();
  170.  
  171.     for (k=0; k<1000; k++) {
  172.         c = cos(k*rotinc*M_PI/180.0);
  173.         s = sin(k*rotinc*M_PI/180.0);
  174.  
  175.           if (verbose) printf ("%4d: (%g, %g)\n", k, c, s);
  176.  
  177.         for (j=0; j<nv; j++) {
  178.             xr = (( xf[j]-CENTER)*c + (yf[j]-CENTER)*s + 0.5);
  179.             yr = ((-xf[j]+CENTER)*s + (yf[j]-CENTER)*c + 0.5);
  180.             x[j] = fp_fix(xr * scale + 105.0);
  181.             y[j] = fp_fix(yr * scale + 32.0);
  182.  
  183.             xi[j] = (int) (xr * scale + 5.5);
  184.             yi[j] = (int) (yr * scale + 32.5);
  185.         }
  186.  
  187.         clear_view_surface(fildes); 
  188.  
  189.           clear_buffer();
  190.         for (i=0; i<nt; i++) {
  191.              if (verbose) 
  192.                 printf("+++++++++++ Drawing triangle %d ++++++++++\n", i);
  193.  
  194.              which_tri = i;
  195.              if (i < nt/2) fill_color(fildes,tc[i].r,tc[i].g,tc[i].b);
  196.              else          fill_color(fildes,1.0, 1.0, 0.5);
  197.  
  198.              triangle(xi[t0[i]], yi[t0[i]],
  199.                          xi[t1[i]], yi[t1[i]], xi[t2[i]], yi[t2[i]]);
  200.  
  201.              if (verbose) printf(" Subpixel: ");
  202.              subpixel_triangle(x[t0[i]], y[t0[i]],
  203.                                      x[t1[i]], y[t1[i]], x[t2[i]], y[t2[i]]);
  204.  
  205.              if (verbose) make_picture_current(fildes);                
  206.         }
  207.  
  208.         if (!verbose) {
  209.              dbuffer_switch(fildes, buf = !buf); 
  210.              make_picture_current(fildes);
  211.           }
  212.  
  213.           if (verbose) {
  214.              printf("hit <return> for next iteration\n");
  215.              getchar();
  216.           }
  217.     }
  218. }
  219.  
  220.  
  221.